package com.example.service;

import cn.hutool.core.util.ObjectUtil;
import com.example.common.Result;
import com.example.common.ResultCode;
import com.example.dao.StudentInfoDao;
import com.example.dao.XueyuanInfoDao;
import com.example.dao.ZhuanyeInfoDao;
import com.example.entity.StudentInfo;
import com.example.entity.XueyuanInfo;
import com.example.entity.ZhuanyeInfo;
import com.example.exception.CustomException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ZhuanyeInfoService {
    @Resource
    private ZhuanyeInfoDao zhuanyeInfoDao;


    @Resource
    private XueyuanInfoDao xueyuanInfoDao;
    @Autowired
    private StudentInfoDao studentInfoDao;

    public void add(ZhuanyeInfo zhuanyeInfo) {
        ZhuanyeInfo info=zhuanyeInfoDao.findByName(zhuanyeInfo.getName());
        if(ObjectUtil.isEmpty(zhuanyeInfo.getDepartment())){
            throw new CustomException("-1","系名不能为空");
        }
        if(ObjectUtil.isEmpty(zhuanyeInfo.getXueyuanid())){
            throw new CustomException("-1","所属学院不能为空");
        }
        if(ObjectUtil.isNotEmpty(info)){
            throw  new CustomException(ResultCode.USER_EXIST_ERROR);
        }

        zhuanyeInfoDao.insertSelective(zhuanyeInfo);
    }


    public List<ZhuanyeInfo> findAll() {
        List<ZhuanyeInfo> list = zhuanyeInfoDao.selectAllZhuanye();
        for(ZhuanyeInfo zhuanyeInfo : list){
            if(ObjectUtil.isNotEmpty(zhuanyeInfo.getXueyuanid())) {
                XueyuanInfo xueyuanInfo = xueyuanInfoDao.selectById(zhuanyeInfo.getXueyuanid());
                zhuanyeInfo.setXueyuanName(xueyuanInfo.getName());
            }
        }
        return list;
    }

    public void update(ZhuanyeInfo zhuanyeInfo) {
        zhuanyeInfoDao.updateById(zhuanyeInfo.getId(),zhuanyeInfo.getName(),zhuanyeInfo.getDepartment(),zhuanyeInfo.getXueyuanid());
    }

    public void deleteById(Long id) {
        zhuanyeInfoDao.deleteById(id);
    }

    public PageInfo<ZhuanyeInfo> findPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ZhuanyeInfo> list = zhuanyeInfoDao.selectAllZhuanye();
        for(ZhuanyeInfo zhuanyeInfo : list){
            if(ObjectUtil.isNotEmpty(zhuanyeInfo.getXueyuanid())) {
                XueyuanInfo xueyuanInfo = xueyuanInfoDao.selectById(zhuanyeInfo.getXueyuanid());
                zhuanyeInfo.setXueyuanName(xueyuanInfo.getName());
                Integer stuNum = studentInfoDao.getstuNum(zhuanyeInfo.getId());
                Integer biyeNum = studentInfoDao.getbiyeNum(zhuanyeInfo.getId());
                double biyelv = (double) biyeNum / stuNum; // 将 biyeNum 强制转换为 double 型
                zhuanyeInfo.setBiyelv(biyelv);
            }
        }
        return PageInfo.of(list);
    }

    public PageInfo<ZhuanyeInfo> findPageName(Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum,pageSize);
        List<ZhuanyeInfo> list =zhuanyeInfoDao.findByNamePage(name);
        for(ZhuanyeInfo zhuanyeInfo : list){
            if(ObjectUtil.isNotEmpty(zhuanyeInfo.getXueyuanid())) {
                XueyuanInfo xueyuanInfo = xueyuanInfoDao.selectById(zhuanyeInfo.getXueyuanid());
                zhuanyeInfo.setXueyuanName(xueyuanInfo.getName());
            }
        }
        return PageInfo.of(list);
    }
}
